App({
    globalData: {
        tcbEnv: '',
        machineInfo: {},

        qMapKey:'',
        mapCenter: {
            longitude: 115.4675865,
            latitude: 38.893923,
        },
        mapScale: 10,

        mapMarker: {
            id: -1,

            latitude: 0,
            longitude: 0,

            title: '',

            iconPath: '/images/bus/blue.png',
            anchor: {
                x: .5,
                y: 58 / 64,
            },
            rotate: 0,
            alpha: 1,
            width: parseInt(44 * 0.8),
            height: parseInt(64 * 0.8),
            zIndex: 1,

            'aria-label': '',

            callout: {
                content: '',
                fontSize: 14,

                color: '#FFFFFF',
                textAlign: 'center',

                borderRadius: 8,
                borderWidth: 8,
                borderColor: '#0072CA',
                bgColor: '#0072CA',

                padding: 1,

                display: 'ALWAYS', //ALWAYS
            },

        },
    },
    onLaunch: function() {
        var privateKeys = require('./privateKeys.js').privateKeys;
        for (var i in privateKeys) {
            this.globalData[i] = privateKeys[i]
            // console.log(i,privateKeys[i]);
        }

        var that = this;
        wx.cloud.init({
            traceUser: true,
            env: that.globalData.tcbEnv
        })
        this.db = wx.cloud.database();

        //update mina
        if (wx.canIUse('getUpdateManager')) {
            const updateManager = wx.getUpdateManager()

            updateManager.onCheckForUpdate(function(res) {
                if (res.hasUpdate) {
                    updateManager.onUpdateReady(function() {
                        wx.showToast({
                            title: '正在启动……',
                            icon: 'loading',
                            duration: 800,
                            mask: true,
                        })

                        updateManager.applyUpdate();
                    })
                }
            })
        }

        //获取屏幕尺寸信息
        wx.getSystemInfo({
            success: function(getSystemInfo_res) {
                that.globalData.machineInfo = getSystemInfo_res;
                wx.setStorage({
                    key: "machineInfo",
                    data: getSystemInfo_res
                })
            }
        })

        //查看当前存储的空间
        wx.getStorageInfo({
            success: function(getStorageInfo_res) {
                if (getStorageInfo_res.currentSize > 8000) {
                    wx.clearStorage();
                }
            }
        })
    },

    deepCopy: function(o) {
        if (o instanceof Array) {
            var n = [];
            for (var i = 0; i < o.length; ++i) {
                n[i] = this.deepCopy(o[i]);
            }
            return n;

        } else if (o instanceof Object) {
            var n = {}
            for (var i in o) {
                n[i] = this.deepCopy(o[i]);
            }
            return n;
        } else {
            return o;
        }
    },

    gnrtKey: function(keyLength) {
        var keySource = 'Il1';

        var key = '';
        for (var i = 0; i < keyLength; i++) {
            key += keySource[Math.floor(Math.random() * keySource.length)];
        }

        return key;
    },

    delEmoji: function(text) {
        var ranges = [
            '\ud83c[\udf00-\udfff]',
            '\ud83d[\udc00-\ude4f]',
            '\ud83d[\ude80-\udeff]'
        ];
        return text.replace(new RegExp(ranges.join('|'), 'g'), '');
    },

    getNowTimestamp() {
        return new Date().getTime();
    },

    getBeijingTime: function(localTs) {
        var time_ts = localTs;
        if (typeof(localTs) != 'number' || localTs == 0) {
            time_ts = this.getNowTimestamp();
        }

        var localDate = new Date(time_ts)
        var ts = time_ts +
            (localDate.getTimezoneOffset() + 8 * 60) * 60 * 1000;
        var time = this.tsToDateStr(ts);

        return {
            ts,
            time,
        };
    },

    tsToDateStr: function(ts_para) {
        var time_ts = 0;
        if (typeof(ts_para) != 'number') {
            time_ts = new Date();
        } else {
            time_ts = new Date(ts_para);
        }

        var time_str = time_ts.getFullYear() + '年';

        if ((time_ts.getMonth() + 1) < 10) {
            time_str += '0';
        }
        time_str += '' + (time_ts.getMonth() + 1) + '月';


        if (time_ts.getDate() < 10) {
            time_str += '0';
        }
        time_str += '' + time_ts.getDate() + '日';


        if (time_ts.getHours() < 10) {
            time_str += '0';
        }
        time_str += '' + time_ts.getHours() + ':';

        if (time_ts.getMinutes() < 10) {
            time_str += '0';
        }
        time_str += '' + time_ts.getMinutes() + ':';

        if (time_ts.getSeconds() < 10) {
            time_str += '0';
        }
        time_str += '' + time_ts.getSeconds(); // + ':' + time_ts.getMilliseconds();

        return time_str;
    },

    tsToNumberString: function(ts_para) {
        var time_ts = 0;
        if (typeof(ts_para) != 'number' || ts_para < 1508098993051) {
            time_ts = this.getNowTimestamp();
        } else {
            time_ts = ts_para;
        }

        var beijingTime = this.getBeijingTime(time_ts).time;
        beijingTime = beijingTime.replace('年', '').replace('月', '')
            .replace('日', '').replace(/:/g, '');

        return beijingTime;
    },

    ifUndefinedString: function(grandfatherObject, fatherTitle, sonStringPropTitle) {
        if (grandfatherObject && grandfatherObject.hasOwnProperty(fatherTitle) &&
            grandfatherObject[fatherTitle].hasOwnProperty(sonStringPropTitle) &&
            grandfatherObject[fatherTitle][sonStringPropTitle].length) {
            return grandfatherObject[fatherTitle][sonStringPropTitle];
        } else {
            return '';
        }
    },

    cleanWhiteSpaces: function(e) {
        return e.replace(/[ ]/g, '');
    },

    saveAppToStorage: function() {
        var that = this;
        wx.setStorage({
            key: "wxUser",
            data: that.globalData.wxUser,
        })
    },

})